package com.cmcc.aregister.ui.adapter;

import java.util.List;

import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.cmcc.aregister.R;
import com.cmcc.aregister.cs.model.Disease;

public class DiseaseAdapter extends BaseAdapter {
	private Context context;
	public List<Disease> list;
	private Handler handler;
	public int currPage;
	public int allPage;

	public DiseaseAdapter(Context context, List<Disease> list, Handler handler,
			int currPage, int allPage) {
		this.context = context;
		this.list = list;
		this.allPage = allPage;
		this.currPage = currPage;
		this.handler = handler;
	}

	public int getCount() {
		int size = list.size();
		if (allPage == 0) {
			allPage = 1;
		}
		if (currPage == 0) {
			currPage = 1;
		}
		if (allPage > currPage) {
			return size + 1;
		} else {
			return size;
		}
	}

	@Override
	public boolean isEnabled(int position) {
		if (position >= list.size()) {
			return false;
		} else {
			return true;
		}
	}

	public Object getItem(int position) {
		return position;
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		if (position >= list.size()) {
			handler.sendMessage(handler.obtainMessage(-1, position));
			convertView = LayoutInflater.from(context).inflate(
					R.layout.list_wait_sm, null);
			convertView.setTag(null);
			convertView.setEnabled(false);
			return convertView;
		} else {
			ViewHolder holder;
			if (convertView != null && convertView.getTag() != null) {
				holder = (ViewHolder) convertView.getTag();
			} else {
				holder = new ViewHolder();
				convertView = LayoutInflater.from(context).inflate(
						R.layout.a_disease, null);
				holder.name = (TextView) convertView.findViewById(R.id.name);
				convertView.setTag(holder);
			}
			String catName=list.get(position).cat_name;
			if(!"".equals(catName) && catName!=null)
			{
			    holder.name.setText(list.get(position).cat_name);
			}
			else {
			    holder.name.setText(list.get(position).ill_name);
            }
			return convertView;
		}
	}

	static class ViewHolder {
		TextView name;
	}
}